<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.id" class="f-item b-b" :class="{active: item.cat_id === currentId}" @click="tabtap(item)">
				{{item.cat_name}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
			<view v-for="item in flist" :key="item.cat_id" class="s-list" :id="'main-'+item.cat_id">
				<view class="t-list">
					<view @click="navToList(item.id, titem.id)" v-if="titem.pid === item.id" class="t-item" v-for="titem in item.category_list" :key="titem.id">
						<image :src="titem.cat_big_pic"></image>
						<text style="line-height: 60rpx;">{{titem.cat_name}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<tabbar class="tabbar" :tabbar="tabbar"></tabbar>
	</view>
</template>

<script>
	var common = require('../../utils/common.js');
	var app = getApp();
	var _this;
	const getCatelist = () => {
		common.post('Appoint_new&a=category', {}, data => {
			console.log("nnnnnnn", data)
			_this.flist = data.category_list;
		});
	};
	export default {
		data() {
			return {
				tabbar: {},
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				cateList:[]
			}
		},
		onShow() {
			common.editTabbar({}, function(data) {
				console.log('index.vue onShow', data)
				_this.tabbar = data;
			});
		},
		onLoad(){
			_this = this;
			getCatelist();
			// this.loadData();
		},
		methods: {
			async loadData(){
				let list = this.cateList;
				list.forEach(item=>{
					if(!item.pid){
						this.tlist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
					}
				})
			},
			//一级分类点击
			tabtap(item){
				if(!_this.sizeCalcState){
					_this.calcSize();
				}
				
				this.currentId = item.cat_id;
				let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.cat_id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(sid, tid){
				uni.navigateTo({
					url: `/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
				})
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 200rpx;
		height: 100%;
		background-color: rgb(255,255,255);
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100rpx;
		font-size: 28rpx;
		color: #000000;
		position: relative;
		&.active{
			color: red;
			background: rgb(245,245,245);
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36rpx;
				width: 8rpx;
				background-color: red;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		overflow: hidden;
		/* padding-left: 20rpx; */
		background-color: rgb(245,245,245);
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		/* background: #fff; */
		padding-top: 12rpx;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		background: #fff;
		/* width: 50%; */
		font-size: 26rpx;
		color: #000000;
		margin-bottom: 20rpx;
		margin-left: 18rpx;
		border-radius: 10rpx;
		overflow: hidden;
		image{
			width: 250rpx;
			height: 140rpx;
		}
	}
</style>
